#!/usr/local/bin/ruby
#-*- coding: UTF-8 -*-
require "fac"
class Dict
  include Fac
  @N = 0
  def initialize(n)
    @N = n
  end

  def nextDictOrder(arr)
    m,n = 0,0

    for i in 1...arr.length do
      if arr[i-1] < arr[i]
        m = i
      end
    end

    for i in m...arr.length do
      if arr[m-1] < arr[i]
        n = i
      end
    end

    arr[m-1],arr[n] = arr[n],arr[m-1]

    len = arr.length-1
    for i in m...(m+arr.length)/2 do
      arr[i],arr[len] = arr[len],arr[i]
      len -= 1
    end

    return arr
  end

  def dictOrder
    arr = Array(1..@N)
    (0...@N).each do |i|  #初始序列
      print "#{arr[i]}"
    end
    puts

    for i in 1...fac(@N) do
      arr = nextDictOrder(arr)
      (0...@N).each do |j|
        print "#{arr[j]}"
      end
      puts
    end
  end

end
